home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / ipacl / matcher.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-12  |  4.6 KB  |  252 lines

  1.  
  2. /* Copyright (C) Gerhard Fuernkranz 1992 */
  3.  
  4. #include "matcher.h"
  5.  
  6. static unsigned short default_prog[] = { (unsigned short) UACCEPT };
  7.  
  8. static unsigned short *global_pc = default_prog;
  9.  
  10. static void
  11. set_pc(unsigned short *pc)
  12. {
  13.     global_pc = pc;
  14. }
  15.  
  16. static int
  17. matcher (unsigned long src, unsigned long dst,
  18.      unsigned short sport, unsigned short dport,
  19.      unsigned short proto)
  20. {
  21.   unsigned short *pc = global_pc;
  22.   unsigned short *ps;
  23.   unsigned long *pl;
  24.   char truth = 0;
  25.   unsigned short n;
  26.  
  27.   if ((OPCODE)(*pc) == UACCEPT)
  28.     return;
  29.  
  30.   for (;;)
  31.     {
  32.       switch ((OPCODE) (*pc++))
  33.     {
  34.     case UACCEPT:
  35.       return 1;
  36.     case UDENY:
  37.     default:
  38.       printf("ipacl: 0x%x 0x%x %d %d %d\n", src, dst, sport, dport, proto);
  39.       printf("ipacl: deny\n");
  40.       return 0;
  41.  
  42.     case TACCEPT:
  43.       if (truth)
  44.         return 1;
  45.       break;
  46.     case FACCEPT:
  47.       if (!truth)
  48.         return 1;
  49.       break;
  50.     case TDENY:
  51.       if (truth)
  52.         {
  53.           printf("ipacl: 0x%x 0x%x %d %d %d\n", src, dst, sport, dport, proto);
  54.           printf("ipacl: deny\n");
  55.           return 0;
  56.         }
  57.       break;
  58.     case FDENY:
  59.       if (!truth)
  60.         {
  61.           printf("ipacl: 0x%x 0x%x %d %d %d\n", src, dst, sport, dport, proto);
  62.           printf("ipacl: deny\n");
  63.           return 0;
  64.         }
  65.       break;
  66.     case TJMP:
  67.       if (truth)
  68.         pc = pc + *(short *) pc;
  69.       else
  70.         pc++;
  71.       break;
  72.     case FJMP:
  73.       if (!truth)
  74.         pc = pc + *(short *) pc;
  75.       else
  76.         pc++;
  77.       break;
  78.     case JMP:
  79.       pc = pc + *(short *) pc;
  80.       break;
  81.  
  82.       /* source address comparison */
  83.  
  84.     case SAEQ:
  85.       pl = (unsigned long *) pc;
  86.       truth = ((src ^ pl[0]) & pl[1]) == 0;
  87.       pc = (unsigned short *) (pl + 2);
  88.       break;
  89.     case SANE:
  90.       pl = (unsigned long *) pc;
  91.       truth = ((src ^ pl[0]) & pl[1]) != 0;
  92.       pc = (unsigned short *) (pl + 2);
  93.       break;
  94.     case SAIN:
  95.       pl = (unsigned long *) (pc + *(short *) pc);
  96.       pc++;
  97.       n = *pc++;
  98.       while (n != 0 && ((src ^ pl[0]) & pl[1]) != 0)
  99.         {
  100.           pl += 2;
  101.           n--;
  102.         }
  103.       truth = (n != 0);
  104.       break;
  105.     case SANIN:
  106.       pl = (unsigned long *) (pc + *(short *) pc);
  107.       pc++;
  108.       n = *pc++;
  109.       while (n != 0 && ((src ^ pl[0]) & pl[1]) != 0)
  110.         {
  111.           pl += 2;
  112.           n--;
  113.         }
  114.       truth = (n == 0);
  115.       break;
  116.  
  117.       /* destination address comparison */
  118.  
  119.     case DAEQ:
  120.       pl = (unsigned long *) pc;
  121.       truth = ((dst ^ pl[0]) & pl[1]) == 0;
  122.       pc = (unsigned short *) (pl + 2);
  123.       break;
  124.     case DANE:
  125.       pl = (unsigned long *) pc;
  126.       truth = ((dst ^ pl[0]) & pl[1]) != 0;
  127.       pc = (unsigned short *) (pl + 2);
  128.       break;
  129.     case DAIN:
  130.       pl = (unsigned long *) (pc + *(short *) pc);
  131.       pc++;
  132.       n = *pc++;
  133.       while (n != 0 && ((dst ^ pl[0]) & pl[1]) != 0)
  134.         {
  135.           pl += 2;
  136.           n--;
  137.         }
  138.       truth = (n != 0);
  139.       break;
  140.     case DANIN:
  141.       pl = (unsigned long *) (pc + *(short *) pc);
  142.       pc++;
  143.       n = *pc++;
  144.       while (n != 0 && ((dst ^ pl[0]) & pl[1]) != 0)
  145.         {
  146.           pl += 2;
  147.           n--;
  148.         }
  149.       truth = (n == 0);
  150.       break;
  151.  
  152.       /* source port comparison */
  153.  
  154.     case SPEQ:
  155.       truth = (proto == pc[0] && sport == pc[1]);
  156.       pc += 2;
  157.       break;
  158.     case SPNE:
  159.       truth = (proto != pc[0] || sport != pc[1]);
  160.       pc += 2;
  161.       break;
  162.     case SPLT:
  163.       truth = (proto == pc[0] && sport < pc[1]);
  164.       pc += 2;
  165.       break;
  166.     case SPGT:
  167.       truth = (proto == pc[0] && sport > pc[1]);
  168.       pc += 2;
  169.       break;
  170.     case SPLE:
  171.       truth = (proto == pc[0] && sport <= pc[1]);
  172.       pc += 2;
  173.       break;
  174.     case SPGE:
  175.       truth = (proto == pc[0] && sport >= pc[1]);
  176.       pc += 2;
  177.       break;
  178.     case SPIN:
  179.       ps = pc + *(short *) pc;
  180.       pc++;
  181.       n = *pc++;
  182.       while (n != 0 && (proto != ps[0] || sport != ps[1]))
  183.         {
  184.           ps += 2;
  185.           n--;
  186.         }
  187.       truth = (n != 0);
  188.       break;
  189.     case SPNIN:
  190.       ps = pc + *(short *) pc;
  191.       pc++;
  192.       n = *pc++;
  193.       while (n != 0 && (proto != ps[0] || sport != ps[1]))
  194.         {
  195.           ps += 2;
  196.           n--;
  197.         }
  198.       truth = (n == 0);
  199.       break;
  200.  
  201.       /* destination port comparison */
  202.  
  203.     case DPEQ:
  204.       truth = (proto == pc[0] && dport == pc[1]);
  205.       pc += 2;
  206.       break;
  207.     case DPNE:
  208.       truth = (proto != pc[0] || dport != pc[1]);
  209.       pc += 2;
  210.       break;
  211.     case DPLT:
  212.       truth = (proto == pc[0] && dport < pc[1]);
  213.       pc += 2;
  214.       break;
  215.     case DPGT:
  216.       truth = (proto == pc[0] && dport > pc[1]);
  217.       pc += 2;
  218.       break;
  219.     case DPLE:
  220.       truth = (proto == pc[0] && dport <= pc[1]);
  221.       pc += 2;
  222.       break;
  223.     case DPGE:
  224.       truth = (proto == pc[0] && dport >= pc[1]);
  225.       pc += 2;
  226.       break;
  227.     case DPIN:
  228.       ps = pc + *(short *) pc;
  229.       pc++;
  230.       n = *pc++;
  231.       while (n != 0 && (proto != ps[0] || dport != ps[1]))
  232.         {
  233.           ps += 2;
  234.           n--;
  235.         }
  236.       truth = (n != 0);
  237.       break;
  238.     case DPNIN:
  239.       ps = pc + *(short *) pc;
  240.       pc++;
  241.       n = *pc++;
  242.       while (n != 0 && (proto != ps[0] || dport != ps[1]))
  243.         {
  244.           ps += 2;
  245.           n--;
  246.         }
  247.       truth = (n == 0);
  248.       break;
  249.     }
  250.     }
  251. }
  252.